package com.ruoyi.platform.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.CouponManagement;
import com.ruoyi.platform.service.ICouponManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 优惠券管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/platform/coupon")
public class CouponManagementController extends BaseController
{
    @Autowired
    private ICouponManagementService couponManagementService;

    /**
     * 查询优惠券管理列表
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(CouponManagement couponManagement)
    {
        startPage();
        List<CouponManagement> list = couponManagementService.selectCouponManagementList(couponManagement);
        return getDataTable(list);
    }

    /**
     * 导出优惠券管理列表
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:export')")
    @Log(title = "优惠券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CouponManagement couponManagement)
    {
        List<CouponManagement> list = couponManagementService.selectCouponManagementList(couponManagement);
        ExcelUtil<CouponManagement> util = new ExcelUtil<CouponManagement>(CouponManagement.class);
        util.exportExcel(response, list, "优惠券管理数据");
    }

    /**
     * 获取优惠券管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") String couponId)
    {
        return success(couponManagementService.selectCouponManagementByCouponId(couponId));
    }

    /**
     * 新增优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:add')")
    @Log(title = "优惠券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CouponManagement couponManagement)
    {
        return toAjax(couponManagementService.insertCouponManagement(couponManagement));
    }

    /**
     * 修改优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:edit')")
    @Log(title = "优惠券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CouponManagement couponManagement)
    {
        return toAjax(couponManagementService.updateCouponManagement(couponManagement));
    }

    /**
     * 删除优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:remove')")
    @Log(title = "优惠券管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable String[] couponIds)
    {
        return toAjax(couponManagementService.deleteCouponManagementByCouponIds(couponIds));
    }

    /**
     * 审核通过优惠券
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:approve')")
    @Log(title = "优惠券审核", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{couponId}")
    public AjaxResult approve(@PathVariable String couponId, @RequestBody CouponManagement couponManagement)
    {
        Long approvalBy = SecurityUtils.getUserId();
        String approvalRemark = couponManagement.getApprovalRemark();
        
        int result = couponManagementService.approveCoupon(couponId, approvalBy, approvalRemark);
        if (result > 0) {
            return AjaxResult.success("审核通过成功");
        } else {
            return AjaxResult.error("审核通过失败");
        }
    }

    /**
     * 拒绝优惠券
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:reject')")
    @Log(title = "优惠券审核", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{couponId}")
    public AjaxResult reject(@PathVariable String couponId, @RequestBody CouponManagement couponManagement)
    {
        Long approvalBy = SecurityUtils.getUserId();
        String rejectReason = couponManagement.getRejectReason();
        
        if (rejectReason == null || rejectReason.trim().isEmpty()) {
            return AjaxResult.error("拒绝原因不能为空");
        }
        
        int result = couponManagementService.rejectCoupon(couponId, approvalBy, rejectReason);
        if (result > 0) {
            return AjaxResult.success("拒绝成功");
        } else {
            return AjaxResult.error("拒绝失败");
        }
    }

    /**
     * 根据商家ID查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:list')")
    @GetMapping("/merchant/{merchantId}")
    public AjaxResult getByMerchantId(@PathVariable String merchantId)
    {
        List<CouponManagement> list = couponManagementService.selectCouponManagementByMerchantId(merchantId);
        return AjaxResult.success(list);
    }

    /**
     * 根据审核状态查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('platform:coupon:list')")
    @GetMapping("/approval/{approvalStatus}")
    public AjaxResult getByApprovalStatus(@PathVariable Integer approvalStatus)
    {
        List<CouponManagement> list = couponManagementService.selectCouponManagementByApprovalStatus(approvalStatus);
        return AjaxResult.success(list);
    }
}